/**
 * 销售报表js
 * @author yangjian
 */
define(function (require, exports) {

    //加载依赖
    require("ajaxproxy");
    var common = require("common");
    var template = require("jtemplate");

    exports.init = function (configs) {

        //初始化AjaxProxy插件
        $('.ajaxproxy').AjaxProxy({
            dataType: "json",
            successCode: "000", //成功时返回的code
            method: "post",
            formId: "J_List_Form",
            formCheckHandler: function () {
                if ($('input[name="ids[]"]:checked').length == 0) {
                    common.message({
                        type: "info",
                        content: "请选择你要操作的记录"
                    });
                    return false;
                }
                return true;
            },
            location: 'reload',
            callbackDelay: __global.jdialog.timer,
            timeInterval: __global.jdialog.timer,
            callBefore: function () {
                return true;
            },
            callBack: function (data) {   /* 执行ajax之后的回调函数 */
                common.ajaxCallback(data);
            }
        });


        $(".js-7-search").click(function () {

            var preDate = getBeforeDate(7); //前7天
            $('input[name=start_time]').val(preDate);
            $('#search-form').submit();

        });
        $(".js-30-search").click(function () {

            var preDate = getBeforeDate(30); //前30天
            $('input[name=start_time]').val(preDate);
            $('#search-form').submit();

        });

    }

});
//获取当前指定的前几天的日期
function getBeforeDate(n) {
    var n = n;
    var d = new Date();
    var year = d.getFullYear();
    var mon = d.getMonth() + 1;
    var day = d.getDate();
    if (day <= n) {
        if (mon > 1) {
            mon = mon - 1;
        }
        else {
            year = year - 1;
            mon = 12;
        }
    }
    d.setDate(d.getDate() - n);
    year = d.getFullYear();
    mon = d.getMonth() + 1;
    day = d.getDate();
    s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
    return s;
}